// 数据库初始化测试脚本

const path = require('path');
const fs = require('fs');

// 测试统一初始化脚本
async function testDatabaseInit() {
  console.log('🔍 测试数据库初始化脚本...');
  
  try {
    // 检查初始化脚本是否存在
    const initSchemaPath = path.join(__dirname, 'init-schema.sql');
    console.log(`检查初始化脚本: ${initSchemaPath}`);
    
    if (fs.existsSync(initSchemaPath)) {
      console.log('✅ 初始化脚本存在');
      
      // 读取脚本内容
      const schemaContent = fs.readFileSync(initSchemaPath, 'utf8');
      console.log(`脚本大小: ${schemaContent.length} 字符`);
      
      // 检查关键表是否存在
      const requiredTables = ['storage_slots', 'goods_info', 'TaskList', 'audit_log', 'tasklist_audit'];
      const requiredTriggers = ['track_tasklist_is_issued_update', 'track_tasklist_is_issued_insert'];
      
      console.log('\n检查必需的表:');
      for (const table of requiredTables) {
        if (schemaContent.includes(`CREATE TABLE IF NOT EXISTS ${table}`) || 
            schemaContent.includes(`CREATE TABLE ${table}`)) {
          console.log(`  ✅ ${table}`);
        } else {
          console.log(`  ❌ ${table} (缺失)`);
        }
      }
      
      console.log('\n检查必需的触发器:');
      for (const trigger of requiredTriggers) {
        if (schemaContent.includes(`CREATE TRIGGER IF NOT EXISTS ${trigger}`) || 
            schemaContent.includes(`CREATE TRIGGER ${trigger}`)) {
          console.log(`  ✅ ${trigger}`);
        } else {
          console.log(`  ❌ ${trigger} (缺失)`);
        }
      }
      
      console.log('\n✅ 数据库初始化脚本检查完成');
    } else {
      console.log('❌ 初始化脚本不存在');
    }
  } catch (error) {
    console.error('测试过程中发生错误:', error);
  }
}

// 运行测试
testDatabaseInit();